<template>
  <div class="login-container">
    <div class="form-wrapper">
      <div>
        <el-form ref="formRef" :rules="data.rules" :model="data.form" style="width: 300px;  margin: 0 auto">
          <div style="margin-bottom: 40px; font-size: 30px; text-align: center; color: #2a7109; font-weight: bold">欢 迎 注 册</div>
          <el-form-item prop="name">
            <el-input style="height: 40px" v-model="data.form.name" autocomplete="off" placeholder="请输入姓名" prefix-icon="User" />
          </el-form-item>
          <el-form-item prop="phone">
            <el-input style="height: 40px" v-model="data.form.phone" autocomplete="off" placeholder="请输入手机号" prefix-icon="Phone" />
          </el-form-item>
          <el-form-item prop="email">
            <el-input style="height: 40px" v-model="data.form.email" autocomplete="off" placeholder="请输入邮箱" prefix-icon="Mail" />
          </el-form-item>
          <el-form-item prop="idCard">
            <el-input style="height: 40px" v-model="data.form.idCard" autocomplete="off" placeholder="请输入身份证号" prefix-icon="Idcard" />
          </el-form-item>
          <el-form-item prop="username">
            <el-input style="height: 40px" v-model="data.form.username" autocomplete="off" placeholder="请输入用户名" prefix-icon="User" />
          </el-form-item>
          <el-form-item prop="password">
            <el-input show-password style="height: 40px"  v-model="data.form.password" autocomplete="off" placeholder="请输入密码" prefix-icon="Lock" />
          </el-form-item>
          <el-form-item prop="confirmPassword">
            <el-input type="password" style="height: 40px"  v-model="data.form.confirmPassword" autocomplete="off" placeholder="请再次确认密码" prefix-icon="Lock" />
          </el-form-item>
        </el-form>
      </div>
      <div style="margin-bottom: 20px">
        <el-button @click="register" size="large" type="primary" style="width: 90%; margin: 0 auto; display: block; margin-top: 30px">注 册</el-button>
      </div>
      <div style="text-align: right">
        已有账号？请<a style="color: #0742b1; text-decoration: none" href="/login">登录</a>
      </div>
    </div>
  </div>
</template>

<script setup>
import {reactive, ref} from "vue";
import {User,Lock} from "@element-plus/icons-vue"
import {ElMessage} from "element-plus";
import router from "@/router/index.js";
import request from "@/utils/request.js";

const validatePass = (rule, value, callback) => {
  if (!value) {
    callback(new Error('请再次确认密码'))
  } else if (value !== data.form.password){
    callback(new Error('两次输入的密码不一致'))
  }else {
    callback()
  }
}
const data = reactive({
  form: {},
  rules: {
    idCard: [
      {required: true, message: '请输入身份证号', trigger: 'blur'},
      {pattern: /^[1-9]\d{5}(19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/, message: '请输入正确的身份证号', trigger: 'blur'}
    ],
    phone: [
      {required: true, message: '请输入手机号', trigger: 'blur'},
      {pattern: /^1[3456789]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur'}
    ],
    email: [
      {required: true, message: '请输入邮箱', trigger: 'blur'},
      {pattern: /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/, message: "请输入邮箱", trigger: 'blur'}
    ],
    name: [
      {required: true, message: '请输入姓名', trigger: 'blur'},
      {min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur'}
    ],
    username: [
      {required: true, message: '请输入用户名', trigger: 'blur'},
      {min: 2, message: '长度应大于 2 个字符', trigger: 'blur'}
    ],
    password: [
      {required: true, message: '请输入密码', trigger: 'blur'},
      {min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur'}
    ],
    confirmPassword: [
      {validator: validatePass, trigger: 'blur'}
    ]
  }
})

const formRef = ref()


const register = () => {
  formRef.value.validate(valid => {
    if (valid) {
      request.post('/register', data.form).then(res => {
        if (res.code == '200') {
          ElMessage.success('注册成功')
          location.href = "/login"
        }else {
          ElMessage.error(res.msg)
        }
      })
    }else {
      ElMessage.warning('请填写正确信息')
    }
  })
}

</script>

<style scoped>.login-container {
  height: 100vh;
  overflow: hidden;
  background-image: url("@/assets/register.jpg");
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;
}

.form-wrapper {
  width: 400px;
  padding: 80px;
  background-color: rgba(255, 255, 255, 0.9);
  border-radius: 20px;
}
</style>